package com.mm.util.common;

import com.mm.base.exception.MuMuException;
import com.mm.domain.book.DBookInfo;
import com.mm.domain.book.DBookSource;
import com.mm.util.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

/**
 * @Description 小说工具类
 * @Date 2023/10/8 下午5:00
 * @Author yanglin
 **/
@Slf4j
public class BookUtil {

    /**
     * 获取链接的document对象
     *
     * @param url
     * @return
     */
    public static Document getDocumentByUrl(String url) {
        boolean flag;
        Document document = null;
        int i = 0;
        do {
            try {
                // 模拟浏览器
                document = Jsoup.connect(url).header("Host", "www.b5200.org").header("Accept",
                                "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language", "zh-CN,zh;q=0.9").header("Cache-Control", "no-cache")
                        .header("Connection", "keep-alive").header("Pragma", "no-cache")
                        .header("Upgrade-Insecure-Requests", "1").timeout(5000)
                        .userAgent("Mozilla")
                        .get();
                flag = false;
            } catch (IOException e) {
                i++;
                long time = RandomUtils.nextInt(2000, 5000);
                log.info("获取document失败重试{}次, 休眠{}毫秒", i, time);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException ex) {
                }
                flag = true;
            }
        } while (flag);
        return document;
    }

    /**
     * 书籍网站源
     *
     * @param bookUrl
     * @param hostUrl
     * @return
     */
    public static Document getBookSourceDocument(String bookUrl, String hostUrl) {
        boolean flag;
        Document document = null;
        int i = 0;
        do {
            try {
                // 模拟浏览器
                document = Jsoup.connect(bookUrl).header("Host", hostUrl).header("Accept",
                                "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language", "zh-CN,zh;q=0.9").header("Cache-Control", "no-cache")
                        .header("Connection", "keep-alive").header("Pragma", "no-cache")
                        .header("Upgrade-Insecure-Requests", "1").timeout(5000)
                        .userAgent("Mozilla")
                        .get();
                flag = false;
            } catch (IOException e) {
                i++;
                if (i > 3) {
                    log.error("getBookSourceDocument=>停止获取document;失败次数：{} url：{}", i, bookUrl);
                    break;
                }
                long time = RandomUtils.nextInt(2000, 5000);
                log.info("getBookSourceDocument=>获取document失败重试{}次, 休眠{}毫秒", i, time);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException ex) {
                }
                flag = true;
            }
        } while (flag);
        return document;
    }


    /**
     * 书籍详情
     *
     * @param bookUrl
     * @param hostUrl
     * @return
     */
    public static Document getBookInfoDocument(String bookUrl, String hostUrl) {
        boolean flag;
        Document document = null;
        int i = 0;
        do {
            try {
                // 模拟浏览器
                document = Jsoup.connect(bookUrl).header("Host", hostUrl).header("Accept",
                                "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language", "zh-CN,zh;q=0.9").header("Cache-Control", "no-cache")
                        .header("Connection", "keep-alive").header("Pragma", "no-cache")
                        .header("Upgrade-Insecure-Requests", "1").timeout(5000)
                        .userAgent("Mozilla")
                        .get();
                flag = false;
            } catch (IOException e) {
                i++;
                if (i > 3) {
                    log.error("getBookInfoDocument=>停止获取document;失败次数：{} url：{}", i, bookUrl);
                    break;
                }
                long time = RandomUtils.nextInt(2000, 5000);
                log.info("getBookInfoDocument=>获取document失败重试{}次, 休眠{}毫秒", i, time);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException ex) {
                }
                flag = true;
            }
        } while (flag);
        return document;
    }
}
